import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', () => {
  // 计数（示例，无关紧要）
  const count = ref(0)

  // 登录用户的信息
  const userId   = ref(null)
  const useType  = ref(null)
  const username = ref('')

  const doubleCount = computed(() => count.value * 2)

  function increment() {
    count.value++
  }

  // 登录后调用
  function setUser({ userId: id, useType: type, username: name }) {
    userId.value   = id
    useType.value  = type
    username.value = name
  }

  // 退出登录时调用
  function logout() {
    userId.value   = null
    useType.value  = null
    username.value = ''
  }

  return {
    // state
    count,
    userId,
    useType,
    username,
    // getters
    doubleCount,
    // actions
    increment,
    setUser,
    logout
  }
})
